#encoding:utf-8
import random
from god import God

class Blank(object):
    def __getattribute__(self,name):
        return 0

class Hero(object):
    '''
    游戏人物
    isOver 0:死亡 1:正常
    self.end 0:结束 1:当前目标 2:待机中
    self.camp 阵营
    
    生命=20(基本点数)+10X8+5X2+5X3+5X3+5X1=145点
    魔力=20(基本点数)+10X1+5X2+5X2+5X2+5X10=110点
    攻击=20(基本点数)+10X0.1+5X2+5X0.2+5X0.2+5X0.1=33
    防御=20(基本点数)+10X0.1+5X0.2+5X2+5X0.2+5X0.1=33
    敏捷=20(基本点数)+10X0.2+5X0.2+5X0.2+5X2+5X0.1=33
    精神=100(基本点数)-10X0.3-5/0.1+5X0.2-5/0.1+5X0.2=101
    
    '''
    def __init__(self,name,vitality,power,strength,intellect,speed,resistance='',exp=0,money=500,camp=0,end=2):
        self.level = 1
        self.name = name
        self._exp = 0
        self.exp = exp
        self.vitality = vitality
        self.power = power
        self.strength = strength
        self.intellect = intellect
        self.speed = speed
        self.resistance = resistance
        self.money = money
        self.condition = set()         #状态(0:正常, 1:中毒, 2:混乱, 3:石化)
        self.harm = 0
        self.position=(0,0)
        self.end = end
        self.god = None
        self.camp = camp
        
        self._vitality = 0
        self._power = 0
        self._strength = 0
        self._intellect = 0
        self._speed = 0
        
        self.weaponsEquip = Blank()
        self.headEquip = Blank()
        self.shoulderEquip = Blank()
        self.clothesEquip = Blank()
        self.legEquip = Blank()
        self.footEquip = Blank()
        
        self._maxhp = 100
        self._maxmp = 50
        self._atk = 10
        self._defend = 6
        self._dexterity = 5
        self._magic = 4
        
        self._hp = self.maxhp
        self._mp = self.maxmp


    def get_equip_list(self):
        return self.weaponsEquip,self.headEquip,self.shoulderEquip,self.clothesEquip,self.legEquip,self.footEquip
    
    @property
    def hp(self):
        return self._hp
    
    @hp.setter
    def hp(self, hp):
        self._hp = hp
        if self._hp > self.maxhp:
            self._hp = self.maxhp
        return self._hp
        
    @property
    def mp(self):
        return self._mp

    @hp.setter
    def mp(self, mp):
        self._mp = mp
        if self._mp > self.maxmp:
            self._mp = self.maxmp
        return self._mp

    @property
    def exp(self):  
        return self._exp
        
    @exp.setter
    def exp(self, exp):
        _exp = self._exp + exp          #最新经验=英雄经验+怪物经验
        for i in range(100):
            _exp = _exp - self.maxexp
            if _exp < 0:
                break
            self.level += 1
            self._exp = _exp
    
    @property
    def maxPoint(self):
        return (self.level-1) * 5
                   
    @property
    def pointed(self):
        return self._vitality + self._power + self._strength + self._intellect + self._speed
        
    @property
    def lavePoint(self):
        return self.maxPoint - self.pointed
    
    @property
    def maxexp(self):
        return (self.level + 1)**4
        
    @property
    def maxhp(self):
        maxhp = self._maxhp + self.vitality*8 + self.power*2 + self.strength*3 + self.speed*3 + self.intellect*1
        maxhp += self._vitality*8 + self._power*2 + self._strength*3 + self._speed*3 + self._intellect*1
        for equip in self.get_equip_list():
            maxhp += equip.vitality*8 + equip.power*2 + equip.strength*3 + equip.speed*3 + equip.intellect*1      
            maxhp += equip.maxhp
        return maxhp
        
    @property
    def maxmp(self):
        maxmp = self._maxmp + self.vitality*1 + self.power*2 + self.strength*2 + self.speed*2 + self.intellect*10
        maxmp += self._vitality*1 + self._power*2 + self._strength*2 + self._speed*2 + self._intellect*10
        for equip in self.get_equip_list():
            maxmp += equip.vitality*1 + equip.power*2 + equip.strength*2 + equip.speed*2 + equip.intellect*10
            maxmp += equip.maxmp
        return maxmp
            
    @property
    def atk(self):
        attack = self._atk + self.vitality*0.1 + self.power*2 + self.strength*0.2 + self.speed*0.2 + self.intellect*0.1
        attack += self._vitality*0.1 + self._power*2 + self._strength*0.2 + self._speed*0.2 + self._intellect*0.1
        for equip in self.get_equip_list():
            attack += equip.vitality*0.1 + equip.power*2 + equip.strength*0.2 + equip.speed*0.2 + equip.intellect*0.1
            attack += equip.atk
        return attack

    @property
    def defend(self):
        defend = self._defend + self.vitality*0.1 + self.power*0.2 + self.strength*2 + self.speed*0.2 + self.intellect*0.1
        defend += self._vitality*0.1 + self._power*0.2 + self._strength*2 + self._speed*0.2 + self._intellect*0.1
        for equip in self.get_equip_list():
            defend += equip.vitality*0.1 + equip.power*0.2 + equip.strength*2 + equip.speed*0.2 + equip.intellect*0.1
            defend += equip.defend
        return defend
        
    @property
    def dexterity(self):
        dexterity = self._dexterity + self.vitality*0.2 + self.power*0.2 + self.strength*0.2 + self.speed*2 + self.intellect*0.1
        dexterity += self._vitality*0.2 + self._power*0.2 + self._strength*0.2 + self._speed*2 + self._intellect*0.1
        for equip in self.get_equip_list():
            dexterity += equip.vitality*0.2 + equip.power*0.2 + equip.strength*0.2 + equip.speed*2 + equip.intellect*0.1
            dexterity += equip.dexterity
        return dexterity
        
    @property
    def magic(self):
        magic = self._magic + self.vitality*0.2 + self.power*0.1 + self.strength*0.2 + self.speed*0.1 + self.intellect*2
        magic += self._vitality*0.2 + self._power*0.1 + self._strength*0.2 + self._speed*0.1 + self._intellect*2
        for equip in self.get_equip_list():
            magic += equip.vitality*0.2 + equip.power*0.1 + equip.strength*0.2 + equip.speed*0.1 + equip.intellect*2
            magic += equip.magic
        return magic

    def setEquip(self, obj):
        '''
        安装装备(0:武器,1:头盔,2:肩膀,3:衣服,4:裤子,5:鞋子)
        '''
        if obj.kind == 0:
            self.weaponsEquip = obj
            return self.weaponsEquip
        elif obj.kind == 1:
            self.headEquip = obj
            return self.headEquip
        elif obj.kind == 2:
            self.shoulderEquip = obj
            return self.shoulderEquip
        elif obj.kind == 3:
            self.clothesEquip = obj
            return self.clothesEquip
        elif obj.kind == 4:
            self.legEquip = obj
            return self.legEquip
        elif obj.kind == 5:
            self.footEquip = obj
            return self.footEquip
    
    def delEquip(self, kind=Blank):
        '''
        卸载装备(0:武器,1:头盔,2:肩膀,3:衣服,4:裤子,5:鞋子)
        '''
        if kind == 0:
            self.weaponsEquip = Blank()
            return self.weaponsEquip
        elif kind == 1:
            self.headEquip = Blank()
            return self.headEquip
        elif kind == 2:
            self.shoulderEquip = Blank()
            return self.shoulderEquip
        elif kind == 3:
            self.clothesEquip = Blank()
            return self.clothesEquip
        elif kind == 4:
            self.legEquip = Blank()
            return self.legEquip
        elif kind == 5:
            self.footEquip = Blank()
            return self.footEquip
        elif kind == Blank:
            data = [self.headEquip, self.shoulderEquip, self.clothesEquip, self.legEquip, self.footEquip]
            for equip in data:
                equip = Blank()
                
    def attributePoint(self,vitality=0,power=0,strength=0,speed=0,intellect=0):
        '''升级属性点'''
        self._vitality = vitality
        self._power = power
        self._strength = strength
        self._intellect = intellect
        self._speed = speed
        return self._vitality + self._power + self._strength + self._intellect + self._speed
    
    def disable(self):
        '''
        return 0:回合结束   1:游戏胜利  2:游戏结束
        '''
        self.end = 0
        # if not [obj for obj,x,y in self.god.objOrderList if obj.camp==1]:
        #     return 1
        # if not [obj for obj,x,y in self.god.objOrderList if obj.camp==0]:
        #     return 2
        # if not [obj for obj,x,y in self.god.objOrderList if obj.end!=0]:
        #     return self.god.nextRound()
        # return self.god.nextObj()
            
    def fly(self, *arg):
        return self.god.fly(self,arg[0],arg[1])
    
    def atkRound(self,enemy,distance=1):
        a,b = self.position
        c,d = enemy.position
        if abs(a-c)+abs(b-d) == distance:
            return True
        return False
    
    def attack(self, enemy):
        '''攻击'''
        if self.atkRound(enemy) and enemy.camp!=0:
            self.harm = int(self.atk*2 - enemy.defend)         #伤害=两倍攻击力-敌人防御力
            if self.harm <= 0:         #伤害小于1时等于1
                self.harm = 1
            self.harm += random.randint(1,3)
            enemy.hp = enemy.hp - self.harm        #hp小于0时等于0
            return enemy.isOver()
        return False

    def counterattack(self,enemy):
        if self.attack(enemy):
            enemy.attack(self)
            return enemy.isOver()
        return 0

    def isOver(self):
        '''判断是否死亡'''
        if self.hp <= 0:
            self.hp = 0
            return 0
        return 1

    def item(self, item, obj=None):
        '''使用物品'''
        if obj == None:
            obj = self
        obj.hp += item.hp
        obj.mp += item.mp
        if item.condition != 0:
            obj.condition.remove(item.condition)
        else:
            obj.condition = set()


class Enemy(object):
    '''
    敌人
    isOver 0:死亡 1:正常
    self.end 0:结束 1:当前目标 2:待机中
    '''
    def __init__(self, level, hp, maxhp, atk, defend, dexterity,name='', item=[], camp=1, end=2):
        self.name = name
        self.level = level
        self.hp = hp
        self.maxhp = maxhp
        self.atk = atk
        self.defend = defend
        self.dexterity = dexterity
        self.money = self.level * 2 + random.randint(1,self.level*2)
        self.harm = 0
        self.position = (0,0)
        self.item = item
        self.camp = camp
        self.end = end
        self.god = None
    
    def fly(self, *arg):
        return self.god.fly(self,arg[0],arg[1])
            
    def disable(self):
        '''
        return 0:回合结束   1:游戏胜利  2:游戏结束
        '''
        self.end = 0
    
    def atkRound(self,enemy,distance=1):
        a,b = self.position
        c,d = enemy.position
        if abs(a-c)+abs(b-d) == distance:
            return True
        return False
    
    def attack(self, enemy):
        '''攻击'''
        if self.atkRound(enemy) and enemy.camp!=1:
            self.harm = int(self.atk*2 - enemy.defend)         #伤害=两倍攻击力-敌人防御力
            if self.harm <= 0:         #伤害小于1时等于1
                self.harm = 1
            self.harm += random.randint(1,3)
            enemy.hp = enemy.hp - self.harm        #hp小于0时等于0
            return enemy.isOver()
        return False
    
    def counterattack(self,enemy):
        if self.attack(enemy):
            return enemy.attack(self)
        return 0

    def isOver(self):
        '''判断是否死亡'''
        if self.hp <= 0:
            self.hp = 0
            self.end = 0
            return 0
        return 1
        
    def __str__(self):
        return self.name
